<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\LangApi;
use Shop\Api\CategoryApi;
use Shop\Api\CategorySkuApi;
use Shop\Api\SkuApi;
use Shop\Api\SkuLangApi;
use Shop\Api\SkuvalueApi;

class SkuController extends AdminController{


	public function index(){

        $lang = I('lang','zh-cn');
		$name = I('name','');
		$params = array(
		    'name'=>$name
		);
		$map = [];
		if(!empty($name)){
		    $map['name'] = ['like','%'.$name.'%'];
        }
				
		$page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));

        $order = " sku.id asc ";
		//
		$result = (new SkuApi())->queryWithLangs($lang,$map,$page,$order,$params);
		//
		if($result['status']){
            $this->assign('lang',$lang);
            $this->assign('name',$name);
            $this->assign("lang_list",LangApi::getLangSupport());
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			$this->display();
		}else{
			LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}
	
	public function add(){
		if(IS_GET){

            $this->assign("lang_list",LangApi::getLangSupport());
			$this->display();
		}else{
			$name = I('name','');
			
			if(empty($name)){
				$this->error("规格不能为空！");
			}
			
			$entity = array(
				'cate_id'=>0,
				'name'=>$name,
			);
			$langs = I('post.cate_sku_lang',[]);
            $langs_value = [];
            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.cate_sku_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }
            $result = (new SkuApi())->addLang($entity,$langs,$langs_value);
			
			
			if($result['status']){
				$this->success("添加成功！",U('Admin/Sku/index'));
			}
			else{
				$this->error($result['info']);
			}
			
		}
	}

	public function edit(){
		$id = I('id','');
		
		if(IS_GET){
			$result = apiCall(SkuApi::GET_INFO,array(array('id'=>$id)));
			if($result['status']){
				$this->assign("vo",$result['info']);
			}
            $result = (new SkuLangApi())->queryNoPaging(['sku_id'=>$id]);
            $this->assign('sku_langs',$result['info']);
			$this->assign("lang_list",LangApi::getLangSupport());
			$this->display();
		}else{
			$name = I('name','');
			
			if(empty($name)){
				$this->error("属性不能为空！");
			}
			
			$entity = array(
				'name'=>$name,
			);
            $langs = I('post.cate_sku_lang',[]);
            $langs_value = [];
            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.cate_sku_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                    if(empty($default_lang_value)){
                        $default_lang_value = $name;
                    }
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }

			$result = (new SkuApi())->updateLang($id,$entity,$langs,$langs_value);

			if($result['status']){
				$this->success("保存成功！",U('Admin/Sku/index'));
			}
			else{
				$this->error($result['info']);
			}
			
		}
	}

	public function deleteCateRelation(){

        $id = I('get.id',0);
        $map = array('sku_id'=>$id);
        $result = (new CategorySkuApi())->delete($map);
        if($result['status']){
            $this->success("清理成功");
        }
        $this->error('清理失败');
    }

	public function delete(){
		$id = I('get.id',0);
		$map = array('sku_id'=>$id);
		$result = apiCall(SkuvalueApi::QUERY_NO_PAGING,array($map));
		if($result['status']){
			if(count($result['info']) > 0){
				$this->error("存在规格值，请先删除所有规格值！");
			}

            $result = (new CategorySkuApi())->queryWithCount($map);
			if(intval($result['info']['count']) > 0){
                $this->error("有类目引用，请删除类目引用关系！");
            }

			$result = (new SkuApi())->deleteLangs($id);
			if($result['status']){
				$this->success("删除成功！");
			}else{
				$this->error($result['info']);
			}
			
		}else{
			
		}
	}

	public function bulkDel(){
        $ids = I('post.ids',0);
        foreach ($ids as $id){
            $result = $this->deleteOneSku($id);
        }
        if($result['status']){
            $this->success("删除成功！");
        }else{
            $this->error($result['info']);
        }
    }

    private function deleteOneSku($sku_id){
        $map = array('sku_id'=>$sku_id);
        $model = M();
        $model->startTrans();
        $result = (new CategorySkuApi())->delete($map);

        $result = (new SkuvalueApi())->queryNoPaging($map);
        foreach ($result['info'] as $item){
            $result = (new SkuvalueApi())->deleteLangs($item['id']);
        }

        $result = (new SkuApi())->deleteLangs($sku_id);
        if($result['status']){
            $model->commit();
        }else{
            $model->rollback();
        }
        return  $result;
    }

    public function forceDelete(){

        $id = I('get.id',0);
        $result = $this->deleteOneSku($id);
        if($result['status']){
            $this->success("删除成功！");
        }else{
            $this->error($result['info']);
        }
    }

}
